package com.teamcalendar.api;

import org.apache.http.HttpEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Base64;

import com.teamcalendar.client.DBAdapter;

public class ParseJson {

	String response;
	DBAdapter mDb;
	String memberId;
	String teamId=null;
	private String eventId;
	private long eventDate;
	private String eventDetails;
	HttpEntity entity;
	String logoType;
	public static String imageUrl;
	String imageName;

	public String getTeamId() {
		return teamId;
	}

	public String getMemberId() {
		return memberId;
	}

	public ParseJson(DBAdapter mDb) {
		super();
		this.mDb = mDb;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public void fromJSONTeam(String team) {
		String name;
		mDb.deleteTable(DBAdapter.DATABASE_TABLE_TEAMS);
		try {
			JSONObject outer = new JSONObject(team);
			if (outer != null) {
				JSONObject user=outer.getJSONObject("user");
				if(user!=null){
					logoType=user.getString("logotype");
					imageUrl=user.getString("imageurl");

					JSONObject outerInner = outer.getJSONObject("teams");
					if (outerInner != null) {
						int length = outerInner.length();
						for (int i = 1; i < length; i++) {
							JSONObject inner = outerInner.getJSONObject("team" + String.valueOf(i));
							if (inner != null) {
								memberId = inner.getString("id");
								name = inner.getString("name");
								imageName=inner.getString("image");
								
								mDb.insertTeam(name, memberId,imageName);							
							}
						}					
					}
					JSONObject outerInner2 = outer.getJSONObject("images");
					if(outerInner2 != null){
						int length=outerInner2.length();
						for (int i = 1; i < length; i++) {
							JSONObject inner = outerInner2.getJSONObject("image" + String.valueOf(i));
							if(inner != null){
								String imageName=inner.getString("name");
								String image=inner.getString("image");
//								byte[] image=Base64.decode(s, Base64.DEFAULT);
								mDb.insertImage(imageName, image);
							}
						}
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void fromJSONMessage(String messages) {
		String senderid = null;
		String message = null;
		String messageId = null;
		boolean readStatus;
		int read = 0;
		try {
			JSONObject outer = new JSONObject(messages);
			mDb.deleteTable(DBAdapter.DATABASE_TABLE_MESSAGES);
			JSONObject outerMember = outer.getJSONObject("messages");
			if (outerMember != null) {
				int length = outerMember.length();
				for (int i = 1; i < length; i++) {
					JSONObject innerMember = outerMember.getJSONObject("message" + String.valueOf(i));
					if (innerMember != null) {
						senderid = innerMember.getString("senderid");
						message = innerMember.getString("messagetext");
						messageId = innerMember.getString("messageid");
						readStatus = innerMember.getBoolean("read");
						if(readStatus) {
							read = 1;
						}
						mDb.insertMessage(senderid, message, messageId, read);
						read = 0;
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}		
	}


	public void fromJSONTeamInfo(String member) {
		String firstName;
		String familyName;
		String address;
		String location;
		String zipCode;
		String phoneNumber;
		String email;
		String role;

		mDb.deleteTable(DBAdapter.DATABASE_TABLE_CURRENTTEAMMEMBERS);
		try {
			JSONObject outer = new JSONObject(member);
			if (outer != null) {
				JSONObject outerInner = outer.getJSONObject("teammembers");
				if (outerInner != null) {
					int length = outerInner.length();
					for (int i=1; i < length; i++) {
						String teammember="teammember"+String.valueOf(i);
						JSONObject inner = outerInner.getJSONObject(teammember);
						if (inner != null) {
							teamId = inner.getString("id");
							firstName = inner.getString("firstname");
							familyName = inner.getString("familyname");
							address = inner.getString("address");
							location = inner.getString("location");
							zipCode = inner.getString("zipcode");
							phoneNumber = inner.getString("phonenumber");
							email = inner.getString("mailaddress");
							role = inner.getString("role");
							imageName=inner.getString("imagename");
							mDb.insertCurrentTeamMember(teamId, firstName, familyName, address, location, zipCode,
									phoneNumber, email, role,imageName);
						}
					}					
				}

				mDb.deleteTable(DBAdapter.DATABASE_TABLE_CURRENTTEAMEVENTS);

				JSONObject outerInnerEvent = outer.getJSONObject("activities");
				if (outerInnerEvent != null) {
					int length = outerInnerEvent.length();
					for (int i = 1; i < length; i++) {
						JSONObject innerEvent = outerInnerEvent.getJSONObject("activity" + String.valueOf(i));
						if (innerEvent != null) {
							eventId = innerEvent.getString("id");
							eventDate = innerEvent.getLong("date");
							eventDetails= innerEvent.getString("activity");

							mDb.insertCurrentTeamEvents(eventId, eventDate, eventDetails);							
						}
					}					
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}	
	public void FromJsonLogin(String register){
		String firstName;
		String lastName;
		String address;
		String location;
		String zipCode;
		String phoneNumber;
		String email;
		String role;
		//		String image;
		String eventId;
		eventDate = 0;
		String eventdetails = null;
		mDb.deleteTable(DBAdapter.DATABASE_TABLE_MYTEAMMEMBERS);
		//mDb.deleteTable(DBA)
		try {
			JSONObject outer = new JSONObject(register);
			if (outer != null) {
				//JSONObject user=outer.getJSONObject("user");
				JSONObject outerInner = outer.getJSONObject("teammembers");
				if (outerInner != null) {
					int length = outerInner.getInt("noofteammembers");
					for (int i=1; i <= length; i++) {
						String teammember="teammember"+String.valueOf(i);
						JSONObject inner = outerInner.getJSONObject(teammember);
						if (inner != null) {
							memberId = inner.getString("id");
							firstName = inner.getString("firstname");
							lastName = inner.getString("familyname");
							address = inner.getString("address");
							location = inner.getString("location");
							zipCode = inner.getString("zipcode");
							phoneNumber = inner.getString("phonenumber");
							email = inner.getString("mailaddress");
							role = inner.getString("role");
							imageName=inner.getString("imagename");
							mDb.insertMyTeamMember(memberId, firstName, lastName, address, location, zipCode,
									phoneNumber, email, role,imageName);
							teamId=inner.getString("teamid");
						}
					}

					JSONObject user=outer.getJSONObject("user");
					if(user!=null){
						memberId = user.getString("teammemberkeyid");

					}
					mDb.deleteTable(DBAdapter.DATABASE_TABLE_MYEVENTS);

					JSONObject events=outer.getJSONObject("activities");
					if(events!=null){
						length=events.getInt("noofactivities");
						for ( int counter=1; counter <= length; counter++) {
							String event="activity"+String.valueOf(counter);
							JSONObject eventObject=events.getJSONObject(event);
							eventId=eventObject.getString("id");
							eventDate=eventObject.getLong("date");
							eventdetails=eventObject.getString("activity");
							mDb.insertMyEvents(eventId,eventDate, eventdetails);
						}
					}
				}
				fromJSONMessage(register);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}



}

